

var flag = 0;

window.onload = function() {
	var modelCode = $("#modelCode").val();
	queryMeterInfo(modelCode);
	$("#modelCode").change(function() {
		var modelCode = $("#modelCode").val();
		queryMeterInfo(modelCode);
	});
	$("#inventory").change(function() {
		calculationPlanAmount();
	});
	$("#perNum").change(function() {
		calculationPlanAmount();
	});
}

function calculationPlanAmount(){
	var inventory = $("#inventory").val();
	var perNum = $("#perNum").val();
	if(inventory != null && inventory != undefined && inventory != ""){
		if(perNum != null && perNum != undefined && perNum != ""){
			$("#planAmount").val(Number(inventory)*Number(perNum));
		}
	}
}

function queryMeterInfo(modelCode){
	var url = basePath + "/bus/meter/queryMeterInfoByModelCode";
	$.ajax({
		type : "post",
		url : url,
		data : "modelCode="+modelCode,
		dataType : "json",
		success : function(data) {
			$("#modelCode").val(data.modelCode);
			$("#useFor").val(data.useFor);
			$("#grow").val(data.grow);
			$("#wide").val(data.wide);
			$("#tall").val(data.tall);
			$("#perNum").val(data.perNum);
			//$("#planAmount").val(data.planAmount);
		}
	});
}

//页面跳转
function toIndex(){
	var url = basePath+"/bus/meter/meterList";
	window.location.href=url;
}

//获取table每一行数据
function getTableValue(){
	//获取计划信息
	var meterTrList = $("#meterTabInfoList tr");
	var meterResult = [];
	if(meterTrList.length>1)
	for (var i=1;i<meterTrList.length;i++) {
		var tdArr = meterTrList.eq(i).find("td");
		var modelCode3 = tdArr.eq(0).html();// 部品番号
		var useFor = tdArr.eq(1).html();//参考耗时
		var grow = tdArr.eq(2).html();
		var wide = tdArr.eq(3).html();
		var tall = tdArr.eq(4).html();
		var perNum = tdArr.eq(5).html();
		var planAmount = tdArr.eq(6).html();
		var inventory = tdArr.eq(7).html();//库存
		var engineering = tdArr.eq(8).html();//工程
		meterResult.push({'modelCode':modelCode3,'useFor':useFor,'grow':grow,'wide':wide,'tall':tall,'perNum':perNum,'planAmount':planAmount,'inventory':inventory,'engineering':engineering});
	}else{
		if(checkInput()){
			var modelCode= $("#modelCode").val();// 部品番号
			var useFor = $("#useFor").val();// 用途
			var grow = $("#grow").val();// 长
			var wide = $("#wide").val();// 幅
			var tall = $("#tall").val();// 板厚
			var perNum = $("#perNum").val();// 每枚可取数
			var planAmount = $("#planAmount").val();//可取台数
			var inventory = $("#inventory").val();//库存
			var engineering = $("#engineering").val();//工程
			var jhdata = {
					modelCode : modelCode,
					useFor : useFor,
					grow : grow,
					wide : wide,
					tall : tall,
					perNum : perNum,
					planAmount : planAmount,
					inventory : inventory,
					engineering : engineering
				};
			meterResult.push(jhdata);
		}else{
			return false;
		}
		}
    $('#addTable').attr("disabled",true);
	var url = basePath+"/bus/meter/addMeter"
		showLoad();//加载层
		$.ajax({
			type : "post",
			url : url,
			data : {meterData:JSON.stringify(meterResult)},
			dataType : "json",
			success : function(data) {
				msgPrompt(data.message);
			}
		});
}

function continueToAddMore(){
		checkIsSumbit()
		flag = 0;
		$("#modelCode").val("");// 番号
		//var useFor = $("#useFor").val("");// 用途
		$("#grow").val("");// 长
		$("#wide").val("");// 幅
		$("#tall").val("");// 板厚
		$("#planNum").val("");// 计划枚数
		$("#perNum").val("");// 每枚可取数
		$("#planAmount").val("");//可取台数
		$("#giveTime").val("");// 下发时间
		$("#operator").val("");// 下发人
		$("#inventory").val("");//库存
		$("#engineering").val("");//工程
	}

function continueToAdd(){
	flag =1
	var modelCode = $("#modelCode").val();// 部品番号
	var useFor = $("#useFor").val();// 用途
	var grow = $("#grow").val();// 长
	var wide = $("#wide").val();// 幅
	var tall = $("#tall").val();// 板厚
	var planNum = $("#planNum").val();// 计划枚数
	var perNum = $("#perNum").val();// 每枚可取数
	var planAmount = $("#planAmount").val();//可取台数
	var giveTime = $("#giveTime").val();// 下发时间
	var operator = $("#operator").val();// 下发人
	var inventory = $("#inventory").val();// 库存
	var engineering = $("#engineering").val();//工程
	if(modelCode == null || modelCode == undefined || modelCode == ""){
		msgFeedBack("部品番号不能为空","#modelCode");
		return false;
	}
	if(useFor == null || useFor == undefined || useFor == ""){
		msgFeedBack("用途不能为空","#useFor");
		return false;
	}
	/*if(engineering == null || engineering == undefined || engineering == ""){
		msgFeedBack("工程不能为空","#engineering");
		return false;
	}*/
	if(grow == null || grow == undefined || grow == ""){
		msgFeedBack("送长不能为空","#grow");
		return false;
	}
	if(wide == null || wide == undefined || wide == ""){
		msgFeedBack("宽幅不能为空","#wide");
		return false;
	}
	if(tall == null || tall == undefined || tall == ""){
		msgFeedBack("板厚不能为空","#tall");
		return false;
	}
	/*if(planNum == null || planNum == undefined || planNum == ""){
		alert("计划枚数不能为空");
		return false;
	}*/
	if(perNum == null || perNum == undefined || perNum == ""){
		msgFeedBack("每枚可取数不能为空","#perNum");
		return false;
	}
	if(planAmount == null || planAmount == undefined || planAmount == ""){
		msgFeedBack("可取台数不能为空","#planAmount");
		return false;
	}
	/*if(giveTime == null || giveTime == undefined || giveTime == ""){
		alert("下发时间不能为空");
		return false;
	}*/
	/*if(operator == null || operator == undefined || operator == ""){
		alert("下发人不能为空");
		return false;
	}*/
	if(inventory == null || inventory == undefined || inventory == ""){
		msgFeedBack("入库数量不能为空","#inventory");
		return false;
	}
	$("#meterInfoList").show();
	$("#meterTabInfoList").append("<tr bordercolor='#FF0000'><td>"+modelCode+"</td><td>"+useFor+"</td><td>"+grow+"</td><td>"+wide+"</td><td>"+tall+"</td><td>"+perNum+"</td><td>"+planAmount+"</td><td>"+inventory+"</td><td><span style = 'cursor:pointer;' onClick = 'deleteRow(this)'>删除</span></td></tr>");
	cleanInput();
}
//保存之后清空表单
function cleanInput(){
	$("#modelCode").val("");// 番号
	$("#grow").val("");// 长
	$("#wide").val("");// 幅
	$("#tall").val("");// 板厚
	$("#planNum").val("");// 计划枚数
	$("#perNum").val("");// 每枚可取数
	$("#planAmount").val("");//可取台数
	$("#giveTime").val("");// 下发时间
	$("#operator").val("");// 下发人
	$("#inventory").val("");//库存
	$("#engineering").val("");//工程
}

//删除当前行

function deleteRow(nowTr){

  //多一个parent就代表向前一个标签,

  // 本删除范围为<td><tr>两个标签,即向前两个parent

  //如果多一个parent就会删除整个table

  $(nowTr).parent().parent().remove();

}

function checkIsSumbit(){
	if(flag == 0){
		if(confirm("您有正在编辑的材料信息未保存是否保存？")){
			flag = 1;
			continueToAdd();
		}else{
			flag = 0;
			$("#modelCode").val("");// 长
			//var useFor = $("#useFor").val("");// 用途
			$("#grow").val("");// 长
			$("#wide").val("");// 幅
			$("#tall").val("");// 板厚
			$("#planNum").val("");// 计划枚数
			$("#perNum").val("");// 每枚可取数
			$("#planAmount").val("");//可取台数
			$("#giveTime").val("");// 下发时间
			$("#operator").val("");// 下发人
			$("#inventory").val("");// 下发人
			$("#engineering").val("");//工程
		}
	}
	return flag;
}



function checkInput(){
	flag =1
	var modelCode = $("#modelCode").val();// 部品番号
	var useFor = $("#useFor").val();// 用途
	var grow = $("#grow").val();// 长
	var wide = $("#wide").val();// 幅
	var tall = $("#tall").val();// 板厚
	var planNum = $("#planNum").val();// 计划枚数
	var perNum = $("#perNum").val();// 每枚可取数
	var planAmount = $("#planAmount").val();//可取台数
	var giveTime = $("#giveTime").val();// 下发时间
	var operator = $("#operator").val();// 下发人
	var inventory = $("#inventory").val();// 库存
	var engineering = $("#engineering").val();//工程
	if(modelCode == null || modelCode == undefined || modelCode == ""){
		msgFeedBack("部品番号不能为空","#modelCode");
		return false;
	}
	if(useFor == null || useFor == undefined || useFor == ""){
		msgFeedBack("用途不能为空","#useFor");
		return false;
	}
	/*if(engineering == null || engineering == undefined || engineering == ""){
		msgFeedBack("工程不能为空","#engineering");
		return false;
	}*/
	if(grow == null || grow == undefined || grow == ""){
		msgFeedBack("送长不能为空","#grow");
		return false;
	}
	if(wide == null || wide == undefined || wide == ""){
		msgFeedBack("宽幅不能为空","#wide");
		return false;
	}
	if(tall == null || tall == undefined || tall == ""){
		msgFeedBack("板厚不能为空","#tall");
		return false;
	}
	/*if(planNum == null || planNum == undefined || planNum == ""){
		alert("计划枚数不能为空");
		return false;
	}*/
	if(perNum == null || perNum == undefined || perNum == ""){
		msgFeedBack("每枚可取数不能为空","#perNum");
		return false;
	}
	if(planAmount == null || planAmount == undefined || planAmount == ""){
		msgFeedBack("可取台数不能为空","#planAmount");
		return false;
	}
	/*if(giveTime == null || giveTime == undefined || giveTime == ""){
		alert("下发时间不能为空");
		return false;
	}*/
	/*if(operator == null || operator == undefined || operator == ""){
		alert("下发人不能为空");
		return false;
	}*/
	if(inventory == null || inventory == undefined || inventory == ""){
		msgFeedBack("剩余库存不能为空","#inventory");
		return false;
	}
	return true
}
